#!/bin/bash

ROOT_PATH=$(cd $(dirname $0) && pwd);

TS_ID=$(xinput list | sed -n "/TouchScreen.*slave  pointer/p" | sed s/^.*id=// | sed s/[^0-9].*$//)
TS_NAME=$(xinput list | sed -n "/TouchScreen.*slave  pointer/p" | sed "s/^.*↳ //" | sed "s/ *\t*id=.*$//")
#BUTTONS_NAME=$(xinput list | sed -n "/2 keyboard.*slave  keyboard/p" | sed "s/^.*↳ //" | sed "s/ *\t*id=.*$//")
BUTTONS_NAME=$(xinput list | sed -n "/extra buttons.*slave  keyboard/p" | sed "s/^.*↳ //" | sed "s/ *\t*id=.*$//")

if [ -n "$TS_ID" ]
then

#xrandr -o inverted
#xinput set-prop $TS_ID 'Coordinate Transformation Matrix' -1 0 1 0 -1 1 0 0 1
#xrandr -o normal
#xinput set-prop $TS_ID 'Coordinate Transformation Matrix' 1 0 0 0 1 0 0 0 1
#xrandr -o left
#xinput set-prop $TS_ID 'Coordinate Transformation Matrix' 0 -1 1 1 0 0 0 0 1
#xrandr -o right
#xinput set-prop $TS_ID 'Coordinate Transformation Matrix' 0 1 0 -1 0 1 0 0 1

#mkdir -p /etc/X11/xorg.conf.d
#touch /etc/X11/xorg.conf.d/99-touchscreen.conf

sudo sh -c "echo '#!/bin/bash

TS_ID=\$(xinput list | sed -n \"/TouchScreen.*slave  pointer/p\" | sed s/^.*id=// | sed s/[^0-9].*\$//)

xinput set-prop \$TS_ID \"Coordinate Transformation Matrix\" 0 1 0 -1 0 1 0 0 1

xrandr -o right
' > /etc/X11/xinit/xinitrc.d/99-touchscreen.sh"

sudo chmod a+x /etc/X11/xinit/xinitrc.d/99-touchscreen.sh

### xinput-calibrator ###

apt -y install xinput-calibrator

### TouchpadEmulator ###

cd /tmp
wget https://gitlab.com/CalcProgrammer1/TouchpadEmulator/-/archive/master/TouchpadEmulator-master.tar

if [ $? -ne 0 ]
then

cp -f "$ROOT_PATH/touchscreen/TouchpadEmulator-master.tar" .

fi

tar -xf TouchpadEmulator-master.tar

if [ -r ./TouchpadEmulator-master/Makefile ]
then

apt -y install build-essential linux-headers-$(uname -r) libdbus-glib-1-dev

cd TouchpadEmulator-master

mv TouchpadEmulator.c TouchpadEmulator.c.bak

sed "s/Goodix Capacitive TouchScreen/$TS_NAME/" TouchpadEmulator.c.bak | sed "s/1c21800.lradc/$BUTTONS_NAME/" |sed "s/int rotation_from_accelerometer_orientation(const char\* orientation)/int rotation_from_accelerometer_orientation(const char* orientation){return(90);} int rotation_from_accelerometer_orientation_legacy(const char* orientation)/" > TouchpadEmulator.c

mv TouchpadEmulator.desktop TouchpadEmulator.desktop.bak
sed "s/^Exec=.*/Exec=TouchpadEmulator/" TouchpadEmulator.desktop.bak | sed "s/^Name=.*/Name=Start Touchpad Emulator/" > TouchpadEmulator.desktop
sed "s/^Exec=.*/Exec=pkill TouchpadEmulator*/" TouchpadEmulator.desktop.bak | sed "s/^Name=.*/Name=Stop Touchpad Emulator/" > StopTouchpadEmulator.desktop

mv Makefile Makefile.bak
sed "s/TouchpadEmulator.desktop/*TouchpadEmulator.desktop/" Makefile.bak > Makefile

make

if [ -r ./TouchpadEmulator ]
then

sudo make install

sh -c "echo '#! /bin/sh

### BEGIN INIT INFO
# Provides:          TouchpadEmulator
# Required-Start:    \$remote_fs
# Required-Stop:     
# Default-Start:     5
# Default-Stop:      
# Short-Description: TouchpadEmulator
# Description:       TouchpadEmulator
### END INIT INFO
' > InitTouchpadEmulator.sh"

sed -n "/pkexec/p" LaunchTouchpadEmulator.sh >> InitTouchpadEmulator.sh
sudo cp -f InitTouchpadEmulator.sh /etc/init.d/InitTouchpadEmulator.sh
sudo chmod a+x /etc/init.d/InitTouchpadEmulator.sh
sudo update-rc.d InitTouchpadEmulator.sh defaults

sudo sh /etc/init.d/InitTouchpadEmulator.sh

sudo sh -c "echo '
TouchpadEmulator &
' >> /etc/X11/xinit/xinitrc.d/99-touchscreen.sh"

fi		# Install TouchpadEmulator

cd ..

fi		# Download TouchpadEmulator

rm -f TouchpadEmulator-master.tar
rm -fdr TouchpadEmulator-master

cd "$ROOT_PATH"

sh /etc/X11/xinit/xinitrc.d/99-touchscreen.sh

fi		# TS_ID
